package com.tmxk.jdbc04.dao.impl;

import com.tmxk.jdbc04.dao.ScheduleDao;
import com.tmxk.jdbc04.po.Schedule;
import com.tmxk.jdbc04.utils.BaseDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 出行计划数据访问层实现类
 */
public class ScheduleDaoImpl extends BaseDao implements ScheduleDao {
    @Override
    public List<Schedule> queryScheduleByType(Map<String, Object> map) {
        //创建List集合对象用来存储出行计划集合数据
        List<Schedule> scheduleList = new ArrayList();
        //创建Schedule对象
        Schedule schedule = null;
        //定义查询sql
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ID,USER_NAME,DUTY,TRIP_MODE_ID,STATUS,TO_CHAR(DEPART_TIME,'yyyy-MM-dd') DEPART_TIME FROM schedule  ");
        sql.append("  WHERE 1 = 1 ");
        if (map != null && map.size() > 0 ) {
            //条件查询 1  出行方式
           // String tripModeId = String.valueOf(map.get("tripModeId")) ;
            Object tripModeId = map.get("tripModeId") ;
            //条件查询2   行程日期
            Object tripTime = map.get("tripTime");
            //条件查询3  行程状态
            Object tripStatus = map.get("tripStatus");
            // 用户只查询条件1
            if ( tripModeId != null  && tripTime == null  ) {
                sql.append(" AND  TRIP_MODE_ID = ? ");
                //调用公共的查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{ map.get("tripModeId") });
            }

            // 用户只查询条件2
            if( tripTime != null  && tripModeId == null ){
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd')  ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{tripTime});
            }

            //  用户查询条件1 +  查询条件2 (组合查询)
            if ( tripModeId != null && tripTime != null ){
                sql.append(" AND  TRIP_MODE_ID = ? ");
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd') ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{tripModeId,tripTime});
            }

            // 条件查询3

            // userName
            if( map.get("userName") != null){
                sql.append(" AND USER_NAME = ?  ");
                rs = super.executeQuery(sql.toString(),new Object[]{ map.get("userName")});
            }
        }else{
            //查询所有
            //调用公共的查询方法
            rs = super.executeQuery(sql.toString());
        }

        try {
            while (rs.next()) {
                //循环遍历rs结果集
                schedule = new Schedule();
                schedule.setId(rs.getInt("ID"));
                schedule.setUserName(rs.getString("USER_NAME"));
                schedule.setDuty(rs.getString("DUTY"));
                schedule.setTripModeId(rs.getInt("TRIP_MODE_ID"));
                schedule.setStatus(rs.getInt("STATUS"));
                schedule.setDepartTime(rs.getString("DEPART_TIME"));
                //将循环遍历的单个对象存储到集合对象中
                scheduleList.add(schedule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return scheduleList;
    }

    @Override
    public int updateScheduleById(Schedule schedule) {
        String sql = " UPDATE  SCHEDULE  SET STATUS = ? WHERE ID = ? ";
        return super.executeUpdate(sql, new Object[]{schedule.getStatus(),schedule.getId()});
    }

    @Override
    public int saveSchedule(Schedule schedule) {
        String sql = " INSERT INTO schedule(ID,USER_NAME,DUTY,TRIP_MODE_ID,STATUS,DEPART_TIME) " +
                " VALUES(SEQ_SCHEDULE_ID.NEXTVAL,?,?,?,?,to_date(?,'yyyy-mm-dd'))";
        return super.executeUpdate(sql,new Object[]{schedule.getUserName(),
                                                schedule.getDuty(),
                                                schedule.getTripModeId(),
                                                schedule.getStatus(),
                                                schedule.getDepartTime()});
    }
}
